package com.muyu.system.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.muyu.common.core.web.domain.BaseEntity;
import com.muyu.system.domain.model.SysMenuAddModel;
import com.muyu.system.domain.model.SysMenuListModel;
import com.muyu.system.domain.model.SysMenuUpdModel;
import com.muyu.system.domain.req.SysMenuAddReq;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;
import lombok.experimental.SuperBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单权限表 sys_menu
 *
 * @author muyu
 */
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@TableName("sys_menu")
public class SysMenu extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 菜单ID
     */
    @TableId(type = IdType.AUTO)
    private Long menuId;

    /**
     * 菜单名称
     */
    @TableField()
    private String menuName;


    /**
     * 父菜单ID
     */
    @TableField()
    private Long parentId;

    /**
     * 显示顺序
     */
    @TableField()
    private Integer orderNum;

    /**
     * 路由地址
     */
    @TableField()
    private String path;

    /**
     * 组件路径
     */
    @TableField()
    private String component;

    /**
     * 路由参数
     */
    @TableField()
    private String query;

    /**
     * 是否为外链（0是 1否）
     */
    @TableField()
    private String isFrame;

    /**
     * 是否缓存（0缓存 1不缓存）
     */
    @TableField()
    private String isCache;

    /**
     * 类型（M目录 C菜单 F按钮）
     */
    @TableField()
    private String menuType;

    /**
     * 显示状态（0显示 1隐藏）
     */
    @TableField()
    private String visible;

    /**
     * 菜单状态（0正常 1停用）
     */
    @TableField()
    private String status;

    /**
     * 权限字符串
     */
    @TableField()
    private String perms;

    /**
     * 菜单图标
     */
    @TableField()
    private String icon;

    /**
     * 子菜单
     */
    @Builder.Default
    @TableField(exist = false)
    private List<SysMenu> children = new ArrayList<>();

    @NotBlank(message = "菜单名称不能为空")
    @Size(min = 0, max = 50, message = "菜单名称长度不能超过50个字符")
    public String getMenuName () {
        return menuName;
    }

    @NotNull(message = "显示顺序不能为空")
    public Integer getOrderNum () {
        return orderNum;
    }

    @Size(min = 0, max = 200, message = "路由地址不能超过200个字符")
    public String getPath () {
        return path;
    }

    @Size(min = 0, max = 200, message = "组件路径不能超过255个字符")
    public String getComponent () {
        return component;
    }

    @NotBlank(message = "菜单类型不能为空")
    public String getMenuType () {
        return menuType;
    }

    @Size(min = 0, max = 100, message = "权限标识长度不能超过100个字符")
    public String getPerms () {
        return perms;
    }

    public static SysMenu of(SysMenuListModel sysMenuListModel) {
        return SysMenu.builder()
                .menuId(sysMenuListModel.getMenuId())
                .menuName(sysMenuListModel.getMenuName())
                .parentId(sysMenuListModel.getParentId())
                .orderNum(sysMenuListModel.getOrderNum())
                .path(sysMenuListModel.getPath())
                .component(sysMenuListModel.getComponent())
                .query(sysMenuListModel.getQuery())
                .isFrame(sysMenuListModel.getIsFrame())
                .isCache(sysMenuListModel.getIsCache())
                .menuType(sysMenuListModel.getMenuType())
                .visible(sysMenuListModel.getVisible())
                .status(sysMenuListModel.getStatus())
                .perms(sysMenuListModel.getPerms())
                .icon(sysMenuListModel.getIcon())
                .children(sysMenuListModel.getChildren())
                .build();
    }

    public static SysMenu buildSysMenu(SysMenuListModel sysMenuListModel) {
        return SysMenu.builder()
                .menuId(sysMenuListModel.getMenuId())
                .menuName(sysMenuListModel.getMenuName())
                .parentId(sysMenuListModel.getParentId())
                .orderNum(sysMenuListModel.getOrderNum())
                .path(sysMenuListModel.getPath())
                .component(sysMenuListModel.getComponent())
                .query(sysMenuListModel.getQuery())
                .isFrame(sysMenuListModel.getIsFrame())
                .isCache(sysMenuListModel.getIsCache())
                .menuType(sysMenuListModel.getMenuType())
                .visible(sysMenuListModel.getVisible())
                .status(sysMenuListModel.getStatus())
                .perms(sysMenuListModel.getPerms())
                .icon(sysMenuListModel.getIcon())
                .children(sysMenuListModel.getChildren())
                .build();
    }

    public static SysMenu buildSysMenu(SysMenuAddReq sysMenuAddReq) {
        return SysMenu.builder()
                .menuId(sysMenuAddReq.getMenuId())
                .menuName(sysMenuAddReq.getMenuName())
                .parentId(sysMenuAddReq.getParentId())
                .orderNum(sysMenuAddReq.getOrderNum())
                .path(sysMenuAddReq.getPath())
                .component(sysMenuAddReq.getComponent())
                .query(sysMenuAddReq.getQuery())
                .isFrame(sysMenuAddReq.getIsFrame())
                .isCache(sysMenuAddReq.getIsCache())
                .menuType(sysMenuAddReq.getMenuType())
                .visible(sysMenuAddReq.getVisible())
                .status(sysMenuAddReq.getStatus())
                .perms(sysMenuAddReq.getPerms())
                .icon(sysMenuAddReq.getIcon())
                .children(sysMenuAddReq.getChildren())
                .build();
    }

    public static SysMenu buildSysMenu(SysMenuAddModel sysMenuAddModel) {
        return SysMenu.builder()
                .menuId(sysMenuAddModel.getMenuId())
                .menuName(sysMenuAddModel.getMenuName())
                .parentId(sysMenuAddModel.getParentId())
                .orderNum(sysMenuAddModel.getOrderNum())
                .path(sysMenuAddModel.getPath())
                .component(sysMenuAddModel.getComponent())
                .query(sysMenuAddModel.getQuery())
                .isFrame(sysMenuAddModel.getIsFrame())
                .isCache(sysMenuAddModel.getIsCache())
                .menuType(sysMenuAddModel.getMenuType())
                .visible(sysMenuAddModel.getVisible())
                .status(sysMenuAddModel.getStatus())
                .perms(sysMenuAddModel.getPerms())
                .icon(sysMenuAddModel.getIcon())
                .children(sysMenuAddModel.getChildren())
                .build();
    }

    public static SysMenu buildSysMenu(SysMenuUpdModel sysMenuUpdModel) {
        return SysMenu.builder()
                .menuId(sysMenuUpdModel.getMenuId())
                .menuName(sysMenuUpdModel.getMenuName())
                .parentId(sysMenuUpdModel.getParentId())
                .orderNum(sysMenuUpdModel.getOrderNum())
                .path(sysMenuUpdModel.getPath())
                .component(sysMenuUpdModel.getComponent())
                .query(sysMenuUpdModel.getQuery())
                .isFrame(sysMenuUpdModel.getIsFrame())
                .isCache(sysMenuUpdModel.getIsCache())
                .menuType(sysMenuUpdModel.getMenuType())
                .visible(sysMenuUpdModel.getVisible())
                .status(sysMenuUpdModel.getStatus())
                .perms(sysMenuUpdModel.getPerms())
                .icon(sysMenuUpdModel.getIcon())
                .children(sysMenuUpdModel.getChildren())
                .build();
    }

}
